package com.eastmoney.emis.edda.controller.inner;

import com.eastmoney.emis.edda.application.vo.StatusUpdateVo;
import com.eastmoney.emis.edda.domain.entity.EddaApply;
import com.eastmoney.emis.edda.domain.entity.EddiInstruction;
import com.eastmoney.emis.edda.domain.repository.HsbcEddaRepository;
import com.eastmoney.emis.edda.domain.repository.HsbcEddiRepository;
import com.eastmoney.emis.edda.domain.valueobject.EddaApplyStatus;
import com.eastmoney.emis.edda.domain.valueobject.EddiStatus;
import com.eastmoney.emis.edda.infrastructure.exception.EddaExceptionStatus;
import com.eastmoney.emis.edda.task.TaskManagement;
import com.eastmoney.emis.edda.task.edda.EddaAuthEnquiryTask;
import com.eastmoney.emis.edda.task.eddi.EddiTask;
import com.eastmoney.emis.utils.common.exception.BusinessException;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: Description
 * @projectName: EDDA-HSBC
 * @package: com.eastmoney.emis.edda.controller.inner
 * @author: 汪程程
 * @date: 2022/10/11 3:11 PM
 * @version: 1.0
 */
@RestController
@RequestMapping("/innerApi/hsbc")
@ApiOperation("HSBC eDDA/eDDI 提交重试接口")
public class ReloadController {

    @Autowired
    private HsbcEddaRepository eddaRepository;
    @Autowired
    private HsbcEddiRepository eddiRepository;
    @Autowired
    private TaskManagement taskManagement;

    @PostMapping("/edda/status")
    public void eddaStatusReload(@Validated @RequestBody StatusUpdateVo statusUpdateVo) {
        EddaApply eddaApply = eddaRepository.findEddaApplyByEid(statusUpdateVo.getEid());
        if (eddaApply == null || EddaApplyStatus.INVALID.name().equals(eddaApply.getStatus())) {
            throw new BusinessException(EddaExceptionStatus.ERROR.code(), "申请不存在或已作废，eid: " + statusUpdateVo.getEid());
        }
        EddaAuthEnquiryTask eddaAuthEnquiryTask = new EddaAuthEnquiryTask(eddaApply);
        eddaAuthEnquiryTask.setProcessUid(statusUpdateVo.getProcessUid());
        eddaAuthEnquiryTask.setProcessUserName(statusUpdateVo.getProcessUserName());
        taskManagement.addTask(eddaAuthEnquiryTask);
    }

    @PostMapping("/eddi/status")
    public void eddiStatusReload(@Validated @RequestBody StatusUpdateVo statusUpdateVo) {
        EddiInstruction eddiInstruction = eddiRepository.findEddiByEid(statusUpdateVo.getEid());
        if (eddiInstruction == null || EddiStatus.INVALID.value().equals(eddiInstruction.getStatus())) {
            throw new BusinessException(EddaExceptionStatus.ERROR.code(), "申请不存在或已作废，eid: " + statusUpdateVo.getEid());
        }
        EddiTask eddiTask = new EddiTask(eddiInstruction);
        eddiTask.setProcessUid(statusUpdateVo.getProcessUid());
        eddiTask.setProcessUserName(statusUpdateVo.getProcessUserName());
        taskManagement.addTask(eddiTask);
    }
}
